<template>
  <div class="elder-detail-container">
    <div class="detail-header">
      <h1>{{ elder.name }} - 老人详情</h1>
      <el-button type="primary" @click="goBack">返回</el-button>
    </div>
    <div class="detail-content">
      <!-- 基本信息卡片：统一性别枚举和bedId字段 -->
      <el-card class="info-card">
        <template #header>
          <div class="card-header"><span>基本信息</span></div>
        </template>
        <el-row :gutter="20">
          <el-col :span="8">
            <div class="info-item">
              <label>性别：</label>
              <span>{{ elder.gender === 1 ? '男' : '女' }}</span> <!-- 统一枚举 -->
            </div>
          </el-col>
          <el-col :span="8">
            <div class="info-item">
              <label>床位号：</label>
              <span>{{ elder.bedId }}</span> <!-- 统一字段：bedId（原bedNumber） -->
            </div>
          </el-col>
        </el-row>
        <!-- 其他信息项不变 -->
      </el-card>
      <!-- 其他卡片不变 -->
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { elderApi } from '@/api/elder' // 修复：用真实接口替换模拟数据

const route = useRoute()
const router = useRouter()
const elder = ref({
  name: '',
  age: 0,
  gender: 1, // 统一枚举
  phone: '',
  bedId: '', // 统一字段
  elderSector: '',
  healthStatus: '',
  specialNeeds: ''
})
const latestMedical = ref({})
const todayMeals = ref([])
const todayMedicines = ref([])
const recentActivities = ref([])

// 修复：从接口加载数据（根据老人ID）
const loadElderDetail = async () => {
  const elderId = route.params.elderId // 从路由获取老人ID
  try {
    // 1. 加载老人基本信息
    const elderRes = await elderApi.getElderById(elderId)
    elder.value = elderRes.data || {}

    // 2. 加载健康档案
    const medicalRes = await elderApi.getLatestMedical(elderId)
    latestMedical.value = medicalRes.data || {}

    // 3. 加载今日餐饮
    const mealRes = await elderApi.getTodayMeals(elderId)
    todayMeals.value = mealRes.data || []

    // 4. 加载今日用药
    const medicineRes = await elderApi.getTodayMedicines(elderId)
    todayMedicines.value = medicineRes.data || []

    // 5. 加载近期活动
    const activityRes = await elderApi.getRecentActivities(elderId)
    recentActivities.value = activityRes.data || []
  } catch (error) {
    console.error('加载老人详情失败:', error)
    ElMessage.error('加载老人详情失败')
  }
}

// 时间格式化和餐型转换不变
const formatTime = (timeStr) => {
  if (!timeStr) return ''
  const date = new Date(timeStr)
  return date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
}
const getMealTypeText = (type) => {
  const mealTypes = { 'breakfast': '早餐', 'lunch': '午餐', 'dinner': '晚餐' }
  return mealTypes[type] || type
}

// 跳转逻辑不变
const showMedicalRecords = () => { router.push(`/medical/${route.params.elderId}`) }
const showMealRecords = () => { router.push(`/meal/${route.params.elderId}`) }
const showMedicineRecords = () => { router.push(`/medicine/${route.params.elderId}`) }
const goBack = () => { router.back() }

onMounted(() => {
  loadElderDetail() // 替换模拟数据加载
})
</script>
<style scoped>
/* 样式不变 */
</style>